package com.giaothong.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.giaothong.R;
import com.giaothong.model.ModelGetDirectionTwoLocationRequests;

public class StepDirectionView extends LinearLayout {
	private View layout;
	private Button btnPrev, btnNext;
	private TextView txtContentStep, txtDistance, txtDuration;
	private ImageView imgShowDirection;
	private ModelGetDirectionTwoLocationRequests modelGetDirectionTwoLocationRequests;
	private int currentDisplay = -1;
	private boolean isShow = false;
	private StepOnclick stepOnclick;
	private DisplayDirectionView displayDirectionView;
	private Context context;

	public StepDirectionView(Context context) {
		super(context);
		init();
	}

	public StepDirectionView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	/**
	 * init view
	 */
	private void init() {
		LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layout = inflater.inflate(R.layout.layout_step_direction, this);
		btnNext = (Button) layout.findViewById(R.id.btnNext);
		btnPrev = (Button) layout.findViewById(R.id.btnPrev);
		txtContentStep = (TextView) layout.findViewById(R.id.txtContentStep);
		txtDistance = (TextView) layout.findViewById(R.id.txtDistance);
		txtDuration = (TextView) layout.findViewById(R.id.txtDuration);
		imgShowDirection = (ImageView) layout.findViewById(R.id.imgShowDirection);

		setAction();

	}

	/**
	 * set Action
	 */
	private void setAction() {
		// click next step
		btnNext.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// hien thi step direction
				txtContentStep.setText(modelGetDirectionTwoLocationRequests.getStep().get(++currentDisplay).getInstructions() + "");
				txtDistance.setText(modelGetDirectionTwoLocationRequests.getStep().get(currentDisplay).getDistance().getText());
				txtDuration.setText("" + modelGetDirectionTwoLocationRequests.getStep().get(currentDisplay).getDuration().getText());
				// hien thi Button prev
				btnPrev.setVisibility(View.VISIBLE);

				// hien thi icon tren map
				stepOnclick.stepOnclick(currentDisplay);
				if (currentDisplay == (modelGetDirectionTwoLocationRequests.getStep().size() - 1)) {
					// neu la step cuoi thi an Button Next
					btnNext.setVisibility(View.INVISIBLE);
				}
			}
		});

		// click prev step
		btnPrev.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				currentDisplay--;
				if (currentDisplay != -1) {
				// hien thi step direction
					txtContentStep.setText(modelGetDirectionTwoLocationRequests.getStep().get(currentDisplay).getInstructions() + "");
					txtDistance.setText(modelGetDirectionTwoLocationRequests.getStep().get(currentDisplay).getDistance().getText() + "");
					txtDuration.setText("" + modelGetDirectionTwoLocationRequests.getStep().get(currentDisplay).getDuration().getText());
				// hien thi Button Next
				btnNext.setVisibility(View.VISIBLE);
				} else {
					// neu la step dau thi an Button Prev
					btnPrev.setVisibility(View.INVISIBLE);
					txtDistance.setText("");
				}

				// hien thi icon tren map
				stepOnclick.stepOnclick(currentDisplay);

			}
		});
		// show or hide Direction
		imgShowDirection.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// neu dang show thi hide
				if (isShow) {
					isShow = false;
					btnNext.setVisibility(View.GONE);
					btnPrev.setVisibility(View.GONE);
					txtContentStep.setVisibility(View.GONE);
					txtDistance.setVisibility(View.GONE);
					txtDuration.setVisibility(View.GONE);
					layout.findViewById(R.id.txtDetail).setVisibility(View.GONE);
					if (displayDirectionView != null)
						displayDirectionView.setVisibility(View.GONE);
				} else {
					// neu dang hide thi show
					isShow = true;
					btnNext.setVisibility(View.VISIBLE);
					btnPrev.setVisibility(View.VISIBLE);
					txtContentStep.setVisibility(View.VISIBLE);
					txtDistance.setVisibility(View.VISIBLE);
					txtDuration.setVisibility(View.VISIBLE);
					layout.findViewById(R.id.txtDetail).setVisibility(View.VISIBLE);
					if (displayDirectionView != null)
						displayDirectionView.setVisibility(View.VISIBLE);
				}
			}
		});
	}

	/**
	 * set Data view direction
	 * 
	 * @param modelGetDirectionTwoLocationRequests
	 */
	public void setData(ModelGetDirectionTwoLocationRequests modelGetDirectionTwoLocationRequests, StepOnclick stepOnclick) {
		this.stepOnclick = stepOnclick;
		this.modelGetDirectionTwoLocationRequests = modelGetDirectionTwoLocationRequests;
		// hien thi diem dau len textview
		txtContentStep.setText(modelGetDirectionTwoLocationRequests.getStartAddress());
		txtDistance.setText("");
		txtDuration.setText("");
		btnPrev.setVisibility(View.INVISIBLE);
		btnNext.setVisibility(View.VISIBLE);
		isShow = true;
	}

	/**
	 * set Display Direction View
	 * 
	 * @param directionView
	 */
	public void setDisPlayDirectionView(DisplayDirectionView directionView, Context context) {
		this.displayDirectionView = directionView;
		this.context = context;
	}

	/**
	 * interface onClickListener
	 */

	public static interface StepOnclick {
		public void stepOnclick(int currentDisplay);
	}


}
